#!/usr/bin/perl

$LOGPATH="/export/radius/$ARGV[1]";

unless ($ARGV[1])
{
	print "Usage search.pl user AAAAMMDD\n";
	exit 0;
}
unless ( stat $LOGPATH )
{
	print "You need to get some money! \n$LOGPATH\n";
	exit 0;
}
searchlog("$ARGV[0]");



###################################################
sub searchlog()
{
my $USER="@_";

my $OKLOG="$LOGPATH/ok.log";
my $FAILLOG="$LOGPATH/fail.log";
print "\n**Iniciando a busca, usuario $USER\n";

open (ARQ, $OKLOG) or die "Error: $OKLOG";
open (ARQ1, $FAILLOG)or die "Error: $FAILOG";
print "\n-pesquisando autenticacoes com sucesso\n";

foreach (<ARQ>)
{
	if ($_ =~ /\s$USER[\@|\'|\s]/)
	{
		push (@match, $_);
	}	
	
}
$totalok=$#match+1;
print "@match[$#match]\n--Total de OK(s) = $totalok\n";
print "--nada encontrado\n" unless @match;
print "\n-pesquisando autenticacoes falhas\n";

foreach (<ARQ1>)
{
        if ($_ =~ /\[$USER\]/)
        {
                push (@err, $_);
        }
}

$totalerro=$#err+1;
print "@err[$#err] \n--Total de falha(s) = $totalerro\n";
print "--nada encontrado\n" unless @err;
print "\n**Fim da busca\n";

close(ARQ);
close(ARQ1);

}
